package com.group.service.impl;

import com.alibaba.fastjson2.JSON;
import com.api.client.IdClient;
import com.api.client.VideoClient;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.domain.dto.GroupAddDTO;
import com.common.domain.dto.GroupItemListDTO;
import com.common.domain.dto.GroupUpdateDTO;
import com.common.domain.po.Group;
import com.common.domain.po.Video;
import com.common.domain.vo.GroupAdminListByPageVO;
import com.common.domain.vo.GroupWebListItemVO;
import com.common.domain.vo.GroupWebListVO;
import com.common.result.Result;
import com.common.utils.BeanUtils;
import com.common.utils.Convert;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.group.mapper.GroupMapper;
import com.group.service.GroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 路亚
 * @version 0.1
 * @className GroupServiceImpl
 * @description
 * @date 2024/12/14 19:32
 * @since jdk11
 */
@Service
@RequiredArgsConstructor
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements GroupService {

    public static final String GROUP_TYPE = "group_list";

    private final IdClient idClient;

    private final GroupMapper groupMapper;

    private final VideoClient videoClient;

    @Override
    public void add(GroupAddDTO dto) {
        Group group = new Group();
        group.setId(idClient.getLongKeyInterval(GROUP_TYPE));
        group.setName(dto.getName());
        group.setCreateTime(LocalDateTime.now());
        group.setRelDj(JSON.toJSONString(dto.getItems()));
        save(group);
    }

    @Override
    public void updateGroup(GroupUpdateDTO dto) {
        Group group = BeanUtils.copyBean(dto, Group.class, new Convert<GroupUpdateDTO, Group>() {
            @Override
            public void convert(GroupUpdateDTO source, Group target) {
                target.setRelDj(JSON.toJSONString(source.getItems()));
            }
        });
        groupMapper.updateById(group);
    }

    @Override
    public List<GroupWebListVO> webList(Integer page) {
        // 1.分页查询所有剧单
        Page<Group> groupPage = new Page<>(page, 10);
        Page<Group> groupPageResult = groupMapper.selectPage(groupPage, null);
        List<Group> groupList = groupPageResult.getRecords();
        // 2.获取
        List<GroupWebListVO> result = new ArrayList<>();
        for (Group group : groupList) {
            GroupWebListVO groupWebListVO = BeanUtils.copyBean(group, GroupWebListVO.class);
            // 处理relDj这个字符串
            Gson gson = new Gson();
            Type listType = new TypeToken<List<GroupWebListItemVO>>() {
            }.getType();
            List<GroupWebListItemVO> list = gson.fromJson(group.getRelDj(), listType);
            List<Long> videoIds = list.stream().map(item -> item.getVideoId()).collect(Collectors.toList());
            List<Video> videos = videoClient.queryByIds(videoIds);
            for (Video video : videos) {
                for (GroupWebListItemVO groupWebListItemVO : list) {
                    if (video.getId().equals(groupWebListItemVO.getVideoId())) {
                        groupWebListItemVO.setVideoName(video.getName());
                        groupWebListItemVO.setVideoImgUrl(video.getCoverUrl());
                        groupWebListItemVO.setTotal(video.getTotalCount());
                    }
                }
            }
            groupWebListVO.setItems(list);
            result.add(groupWebListVO);
        }
        return result;
    }

    @Override
    public GroupAdminListByPageVO adminList(Integer page) {
        Page<Group> groupPage = new Page<>(page, 10);
        Page<Group> groupPageResult = groupMapper.selectPage(groupPage, null);
        return new GroupAdminListByPageVO(groupPageResult.getTotal(), groupPageResult.getPages(), groupPageResult.getRecords(), groupPageResult.getRecords().isEmpty());
    }
}
